package top.xchch.tool.mockito.juejin;

import org.junit.Test;
import org.mockito.ArgumentMatchers;

import java.util.LinkedList;

import static org.mockito.Mockito.*;

/**
 * 参数匹配
 */
public class ArgumentMatcherTest03 {
    @Test
    public void testArgumentMatcher() throws Exception {
        LinkedList mockedList = mock(LinkedList.class);
        //用内置的参数匹配器来stub
        when(mockedList.get(ArgumentMatchers.anyInt())).thenReturn("element");
        when(mockedList.get(33)).thenReturn("element");

        //打印 "element"
        System.out.println(mockedList.get(999));
        System.out.println(mockedList.get(33));

        //你也可以用参数匹配器来验证，此处测试通过
        verify(mockedList, times(2)).get(ArgumentMatchers.anyInt());
        verify(mockedList).get(ArgumentMatchers.eq(33));
        //此处测试将不通过，因为没调用get(77)
        verify(mockedList).get(ArgumentMatchers.eq(77));
    }

}
